﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.WebPages;

namespace MvcToolbox
{
    public static class ScriptExtension
    {
        /// <summary>
        /// Scripts the document ready.
        /// </summary>
        /// <example>@Html.ScriptDocumentReady(@<text>alert('ok');</text>)</example>
        /// <typeparam name="TModel">The type of the T model.</typeparam>
        /// <param name="html">The HTML.</param>
        /// <param name="render">The render.</param>
        /// <returns>IHtmlString.</returns>
        public static IHtmlString ScriptDocumentReady<TModel>(this HtmlHelper<TModel> html, Func<object, HelperResult> render)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<script>");
            sb.AppendLine("$(document).ready(function() {");
            sb.AppendLine(render(null).ToHtmlString());
            sb.AppendLine("});");
            sb.AppendLine("</script>");
            return new HtmlString(sb.ToString());
        }
    }
}